package DAY1209.Demo4;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Author:LiRegin
 * @Date: 2019/12/9 17:21:12
 */
public class Test {
   static ArrayList<Phone> list=new ArrayList<Phone>();
    public static void main(String[] args) throws DocumentException {
        inn();
        System.out.println("【查询手机】\n1.黑色\t2.白色\t3.银色\t4.金色\t5.退出");
        System.out.println("请输入查询项");
        while (true) {
            switch (new Scanner(System.in).nextInt()) {
                case 1:
                    findPhone("黑色");
                    break;
                case 2:
                    findPhone("白色");
                    break;
                case 3:
                    findPhone("银色");
                    break;
                case 4:
                    findPhone("金色");
                    break;
                case 5:
                   return;
            }
        }
    }
    public static ArrayList<Phone> inn() throws DocumentException {
        SAXReader reader = new SAXReader();
        Document dom = reader.read(new File("C:\\XML\\src\\DAY1209\\Demo4\\phones.xml"));
        Element root = dom.getRootElement();
        List<Element> servletList = root.elements("product");
        for(Element servlet:servletList) {
            List<Element> elements = servlet.elements();
            list.add(new Phone(elements.get(0).getText(),elements.get(1).getText(),
                    Double.parseDouble(elements.get(2).getText()),
                    Double.parseDouble(elements.get(3).getText()),elements.get(4).getText()));
        }
        return list;
    }
    public static void findPhone(String a){
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getColor().equals(a)){
                System.out.println(list.get(i));
                return;
            }
        }
        System.out.println("没有该颜色手机");
    }
}
